package com.zwh.minio.config;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @author zwh
 * @date 2024-3-25 17:36
 */
@Data
@Component
@ConfigurationProperties(prefix = "minio")
public class MinioConfig {

    private String endpoint;

    private String accesskey;

    private String secretkwy;

    private String rootBucket;
    /**
     * 获取 MinioClient
     *
     * @return MinioClient
     */
    @Bean
    public MinioClient minioClient() {
        MinioClient minioClient = MinioClient.builder().endpoint(endpoint).
                credentials(accesskey, secretkwy).build();
        try {
            boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(rootBucket).build());
            if (!found) {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(rootBucket).build());
            }
        } catch (Exception e) {
            throw new RuntimeException("minio 初始化失败");
        }
        return minioClient;
    }
}
